package com.fyy.quartz.listener;

import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.listeners.TriggerListenerSupport;
import org.springframework.stereotype.Component;

/**
 * @Author fyy
 * @Description 创建任务触发监听器
 * @Date 2020/12/23 14:42
 */
@Component
public class SimpleTriggerListener extends TriggerListenerSupport {

    @Override
    public String getName() {
        return "mySimpleTriggerListener";
    }

    //Trigger被激发 它关联的job即将被运行
    @Override
    public void triggerFired(Trigger trigger, JobExecutionContext context) {
        System.out.println("myTriggerListener.triggerFired()");
    }

    //Trigger被激发 它关联的job即将被运行,
    // TriggerListener 给了一个选择去否决 Job 的执行,
    // 如果返回TRUE 那么任务job会被终止
    @Override
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
        System.out.println("myTriggerListener.vetoJobExecution()");
        return false;
    }

    //当Trigger错过被激发时执行,比如当前时间有很多触发器都需要执行，
    // 但是线程池中的有效线程都在工作，
    //那么有的触发器就有可能超时，错过这一轮的触发。
    @Override
    public void triggerMisfired(Trigger trigger) {
        System.out.println("myTriggerListener.triggerMisfired()");
    }

    //任务完成时触发
    @Override
    public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
        System.out.println("myTriggerListener.triggerComplete()");
    }
}
